package com.group.three.controller;

import com.github.pagehelper.PageInfo;
import com.group.three.pojo.VideoImages;
import com.group.three.pojo.VideoTag;
import com.group.three.pojo.Videos;
import com.group.three.pojo.dto.AddNewVideoDto;
import com.group.three.pojo.vo.JsonResult;
import com.group.three.service.VideoImagesService;
import com.group.three.service.VideoTagService;
import com.group.three.service.VideosService;
import com.group.three.utils.SnowFlakeUtil;
import com.group.three.utils.TimeUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.soap.SOAPBinding;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class VideosController {
    @Autowired
    private VideosService videosService;

    @Autowired
    private VideoTagService videoTagService;

    @Autowired
    private VideoImagesService videoImagesService;

    @RequestMapping("/videos")
    public ModelAndView modelAndView(ModelAndView modelAndView) {
        List<Videos> videos = videosService.selectVideosAll();
        for (Videos video : videos) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String time =format.format(video.getVideoOntime());
            video.setVideoStrLength(TimeUtils.formatTime(video.getVideoLength()));
           video.setNewVideoTime(time);

        }
        modelAndView.addObject("videos", videos);
        PageInfo pageInfo =new  PageInfo(videos);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("/3-admin/movie_list");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/deleteVideo")
    public JsonResult videos(Long videoId) {
        int deleteVideoId = videosService.deleteVideoId(videoId);
        if (deleteVideoId <= 0) {
            JsonResult jsonResult = new JsonResult("0", "失败");
            return jsonResult;
        }
        JsonResult jsonResult = new JsonResult("200", "成功");
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/videoSearch")
    public JsonResult searchByInput(String input) {
        List<Videos> videos = videosService.searchByInput(input);
        for (Videos video : videos) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String time =format.format(video.getVideoOntime());
            video.setVideoStrLength(TimeUtils.formatTime(video.getVideoLength()));
            video.setNewVideoTime(time);

        }
        return new JsonResult("200", "success", videos);
    }

    @RequestMapping(value = "/videoPage")
    public ModelAndView videoPage(Map<String, Object> map, HttpSession session, ModelAndView modelAndView,
                                     @RequestParam(defaultValue = "1") int page,
                                     @RequestParam(defaultValue = "5") int size){
        List<Videos> list= videosService.selectAll(page,size);
        for (Videos video : list) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String time =format.format(video.getVideoOntime());
            video.setVideoStrLength(TimeUtils.formatTime(video.getVideoLength()));
            video.setNewVideoTime(time);

        }
        PageInfo pageInfo =new  PageInfo(list);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("3-admin/movie_list");
        return modelAndView;
    }


    //====================================添加视频==============================================

    @RequestMapping("/goAddMovie")
    public ModelAndView goAddMovie(ModelAndView modelAndView,HttpSession session) {
        Object adminId = session.getAttribute("adminId");
        if (adminId ==""|| adminId ==null){
            modelAndView.setViewName("/3-admin/login");
            return modelAndView;
        }
        PageInfo<VideoTag> videoTagPageInfo = videoTagService.selectAllTag(1, 8);
        modelAndView.addObject("tagList",videoTagPageInfo.getList());
        modelAndView.setViewName("/3-admin/movie_add");
        return modelAndView;
    }

    @SneakyThrows
    @RequestMapping("/upload")
    @ResponseBody
    public JsonResult uploadProductImage(MultipartFile uploadFile, HttpSession session, String fileType) {

        String fileName = uploadFile.getOriginalFilename();
        String hzName = fileName.substring(fileName.lastIndexOf("."));
        String fileUUid =UUID.randomUUID().toString();
        fileName =  fileUUid+ hzName;
        ServletContext servletContext = session.getServletContext();//用于获取服务器路径

        String filePath = "";
        String localPath = "";
        //对上传的文件进行判断
        if (fileType.equals("video")) {
            localPath = "/resource/static/video";
            filePath = servletContext.getRealPath(localPath);
        } else {
            localPath = "/resource/static/base/images";
            filePath = servletContext.getRealPath(localPath);
        }


        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdir();
        }
        String finalPath = filePath + File.separator + fileName;
        //实现上传功能
        file = new File(finalPath);


        uploadFile.transferTo(file);
        Map<String, String> data = new HashMap<>();
        //获取视频时长
        if (fileType.equals("video")) {
            Long videoTime = TimeUtils.getVideoTime(file);
            data.put("input_length_str",TimeUtils.formatTime(videoTime));
            data.put("input_length", videoTime.toString());
        }

        data.put("src", localPath + "\\" + fileName);
        data.put("fileName", fileName);
        data.put("fileType",fileType);
        data.put("fileUUid",fileUUid);

        return new JsonResult("200", "success", data);
    }


    @RequestMapping("/addNewVideo")
    @ResponseBody
    public JsonResult addNewVideo(AddNewVideoDto addNewVideoDto,VideoImages videoImages){
        Long snowFlakeId = SnowFlakeUtil.getSnowFlakeId();
        addNewVideoDto.setVideo_id(snowFlakeId);
        videoImages.setIamgeId(SnowFlakeUtil.getSnowFlakeId());
        videoImages.setImageState(0);
        videoImages.setImageName(addNewVideoDto.getImageName());
        videoImages.setImageVideoId(snowFlakeId);
        boolean b1 = videosService.addNewVideo(addNewVideoDto);
        boolean b = videoImagesService.addNewImage(videoImages);

        if (b1&b){
            return new JsonResult("200","success");
        }else {
            return new JsonResult("400","error");
        }

    }

    }
